module ControllerHelper
  def merge_conditions condition1, condition2, conjunction = 'AND'
    if Array === condition2
      if condition1.empty?
        condition1 = condition2
      else
        condition1[0] = "(#{condition1[0]}) #{conjunction} (#{condition2[0]})" unless condition1.empty?
        condition2.each_with_index do |item,index|
          puts "index: #{index}; item: #{item};"
          condition1 << item if index > 0
        end
      end
    else
      raise "don't know how to handle this condition type"
    end
    puts "condition: #{condition1}"
    condition1
  end

  def random_string size
    (1..size).map{|i| ('a'..'z').to_a[rand(26)]}.join
  end

  def get_checkboxes_csv entity, form_name, method_name
    values = entity.send("#{method_name}s").collect{|item| I18n.t("#{form_name}.#{method_name}.#{item.name}")}
    custom = entity.send("#{method_name}_custom")
    values << custom unless custom.empty?
    values.join(",")
  end

  def get_ordered_shops
    order_products = Product.find(:all, :conditions => ["id IN (?)", @items.keys])
    order_categors = Categor.find(:all, :conditions => ["id IN (?)", order_products.collect do |product| product.categor_id end])
    @order_shops = Shop.find(:all, :conditions => ["id IN (?)", order_categors.collect do |categor| categor.shop_id end])
    @order_shops.each do |shop|
      order_categors.each do |categor|
        shop.add_order_categor categor if categor.shop_id == shop.id
      end
    end
    order_categors.each do |categor|
      order_products.each do |product|
        categor.add_order_product product if product.categor_id == categor.id
      end
    end
  end
  
  def get_page_cont
    @pagecont = Pagecont.find(:first, :conditions => ["name = ?", params[:action]])
    @page["bonus_title"] = @pagecont.title
    @page["bonus_keywords"] = @pagecont.keywords
    @page["bonus_description"] = @pagecont.description
  end

  def get_hrn price
    "%.2f #{I18n.t("orders.excel.price.suffix")}" % price if price
  end

end
